import re

import requests
from lxml import etree

counter = 0
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
}
url = "https://search.suning.com/emall/searchV1Product.do"
for cp in range(0, 29 + 1):
    for paging in range(0, 3 + 1):
        params = {
            "keyword": "空调",
            "ci": "431505",
            "pg": "01",
            "cp": cp,
            "iy": "-1",
            "n": "1",
            "sesab": "ACBACBEBBAEABB",
            "id": "IDENTIFYING",
            "cc": "371",
            "paging": paging,
            "jzq": 3376,
        }
        response = requests.get(url, headers=headers, params=params)
        root = etree.HTML(response.content.decode())

        div_list = root.xpath("//div[@class='item-bg']")
        for div in div_list:
            counter += 1
            title = "".join(div.xpath(".//div[@class='title-selling-point']/a/text()"))
            title = re.sub("\s", "", title)
            detail_url = "".join(div.xpath(".//div[@class='title-selling-point']/a/@href"))
            comment_count = "".join(div.xpath(".//div[@class='info-evaluate']/a/i/text()"))
            print(cp, paging, counter, title, detail_url, comment_count)
